package com.example.etwzgzxt.view.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.etwzgzxt.R;
import com.example.etwzgzxt.bean.Info;
import com.example.etwzgzxt.tool.HttpHelper;
import com.example.etwzgzxt.tool.InfoApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.message.BasicNameValuePair;

public class InfoActivity extends AppCompatActivity {

    private static final int GET_INFO = 1;

    private View.OnClickListener navListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            InfoActivity.this.finish();
        }
    };
    InfoApplication infoApplication;
    private Info info;

    private TextView titleRealName;
    private TextView tvRealName, tvGender, tvOccupation, tvAddress,  tvEmail, tvQQ;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            toolbar.setNavigationIcon(R.drawable.ic_toolbar_arrow_back);
            toolbar.setNavigationOnClickListener(navListener);
        }
        infoApplication = (InfoApplication) getApplication();
        titleRealName = (TextView) findViewById(R.id.titleRealName);
        tvRealName = (TextView) findViewById(R.id.tvRealName);
        tvGender = (TextView) findViewById(R.id.tvGender);
        tvOccupation = (TextView) findViewById(R.id.tvOccupation);
        tvAddress = (TextView) findViewById(R.id.tvAddress);
        tvEmail = (TextView) findViewById(R.id.tvEmail);
        tvQQ = (TextView) findViewById(R.id.tvQQ);
        TextView btnEdit = (TextView) findViewById(R.id.btnEdit);
        btnEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(InfoActivity.this, InfoUpdateActivity.class));
            }
        });
    }

    public void initView() {
        try {
            titleRealName.setText(info.getRealName());
            tvRealName.setText(info.getRealName());
            tvGender.setText(info.getGender());
            tvOccupation.setText(info.getOccupation());
            tvAddress.setText(info.getAddress());
            tvEmail.setText(info.getEmail());
            tvQQ.setText(info.getQQ());
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    public class BackgroundTask extends AsyncTask<Void, Void, String> {

        int type;

        public BackgroundTask(int type) {
            this.type = type;
        }

        @Override
        protected String doInBackground(Void... params) {
            String baseURL =  ((InfoApplication)getApplicationContext()).getBasehost()+"/user/profile/get.do";
            NameValuePair pair = new BasicNameValuePair("access_token", ((InfoApplication) getApplication()).getAccess_token());
            List<NameValuePair> pairList = new ArrayList<>();
            pairList.add(pair);
            return new HttpHelper().post(baseURL, pairList);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
                parseResult(result);
        }
    }

    public void parseResult(String result) {
        progressDialog.dismiss();
        if (result == null) {
            Toast.makeText(this, "请检查网络设置", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject(result);
            switch (jsonObject.getInt("code")) {
                case 200:
                    jsonObject = new JSONObject(jsonObject.getString("result"));
                    info = new Info(jsonObject.optString("address"),jsonObject.optString("roleid"),  jsonObject.optString("company"),
                            jsonObject.optString("email"),  jsonObject.optString("gender"), jsonObject.optString("occupation"),
                            jsonObject.optString("phone"), jsonObject.optString("qq"), jsonObject.optString("realname"), null);
                    System.out.println(info.toString());
                    infoApplication.setInfo(info);
                    initView();
                    break;
                case 403:
                    Toast.makeText(this, "Not Acceptable", Toast.LENGTH_SHORT).show();
                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        progressDialog = ProgressDialog.show(this, null, null);
        new BackgroundTask(GET_INFO).execute();
    }
}
